﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CDSysLDManageProject
{
    /// <summary>
    /// 分包初解析
    /// </summary>
    public class CDPackPick
    {
        List<string> ecsmsg=new List<string>();
        public void bufferHandle(List<byte> RecBufferList)
        {
            int firstStart = 0;//定义第一个包头
            int firstEnd = 0;
        
            if (RecBufferList.Count >= 18)
            {
                //string str1 =Encoding.ASCII.GetString(RecBufferList.ToArray());
                for (firstStart = 0; firstStart < RecBufferList.Count - 1; firstStart++)//从新数组中检索包头
                {
                    if (RecBufferList[firstStart] == 0x02)
                    {
                      
                        break;
                    }
                }
                //寻找包尾索引
                for (firstEnd = 0; firstEnd < RecBufferList.Count - 1; firstEnd++)
                {
                    if (RecBufferList[firstEnd] == 0x03)
                    {
                      
                        break;
                    }
                }

                if (firstEnd - firstStart > 0)
                {
                    List<byte> datahandle = new List<byte>();
                    //datahandle = bufferList.GetRange(firstStart, 2231);
                    int datalength = firstEnd - firstStart + 1;
                    byte[] data = new byte[datalength];

                   
                    Buffer.BlockCopy(RecBufferList.ToArray(), firstStart, data, 0, datalength);

                    RecBufferList.RemoveRange(0, firstEnd + 1);//清空掉大List中取走的数据

                    byteSubString(data);//截取并存入数据库
                    // int ll = bufferList.Count;
                }
                else if (firstStart == firstEnd)//前112或更多个没有需要数据时
                {
                    RecBufferList.RemoveRange(0, firstEnd + 1);//直接丢弃

                }
                else if (firstEnd - firstStart <= 0)
                {
                    RecBufferList.RemoveRange(0, firstEnd + 1);//直接丢弃
                }

            }
        }

        private void byteSubString(byte[] data)
        {
            string resultStr = Encoding.ASCII.GetString(data);
            string iCommand = resultStr.Substring(1, 3);
            
            if (GParam.RecStringDic.ContainsKey(iCommand))
            {
                    GParam.RecStringDic[iCommand] = resultStr;
               
                   
                    
                  Log.ReciveFromECS.Add("Command :" + iCommand + " ResultStr: " + resultStr);
            }else
            {
                GParam.RecStringDic.Add(iCommand, resultStr);
               
                Log.ReciveFromECS.Add("Command :" + iCommand + " ResultStr: " + resultStr);
            }
            if (GParam.RecListDic.ContainsKey(iCommand))
            {
                GParam.RecListDic[iCommand].Add(resultStr);
              
            }
           
        }
    }
}
